package com.hq.ims.data.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hq.ims.data.config.verification.VdAdd;
import com.hq.ims.data.config.verification.VdEdit;
import com.hq.ims.data.dao.BusiRepeatTypeCountDataMapper;
import com.hq.ims.data.entity.BusiRepeatTypeCount;
import com.hq.ims.data.entity.BusiRepeatTypeCountData;
import com.hq.ims.data.entityenum.EnumBusinessRepeatType;
import com.hq.ims.data.vo.BusiRepeatTypeCountDataMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.utils.util.ObjectUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 蒋柏平
 * @detail 重复率表 服务实现类
 * @since 20241025
 */
@Service
public class BaseBusiRepeatTypeCountDataService extends BaseService<BusiRepeatTypeCountDataMapper, BusiRepeatTypeCountData> {

    @Resource
    private BaseBusiRepeatTypeCountService baseBusiRepeatTypeCountService;

    /**
     * 分页
     */
    public List<BusiRepeatTypeCountDataMapstruct.BusiRepeatTypeCountDataVo> getByPage(
            RequestPage page,
            BusiRepeatTypeCountDataMapstruct.PageSearchVo searchVo
    ) {
        //验证
        LambdaQueryChainWrapper<BusiRepeatTypeCountData> lq = this.lambdaQuery();
        //排序
        //查询条件

        //执行分页
        lq.page(page);

        List<BusiRepeatTypeCountData> dataList = page.getRecords();
        List<BusiRepeatTypeCountDataMapstruct.BusiRepeatTypeCountDataVo> voList = BusiRepeatTypeCountDataMapstruct.INSTANCE.toBusiRepeatTypeCountDataVoList(dataList);
        page.setRecords(voList);
        return voList;
    }

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    public BusiRepeatTypeCountData addData(BusiRepeatTypeCountDataMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdAdd.class);

        //账值新的对象
        BusiRepeatTypeCountData entity = new BusiRepeatTypeCountData();
        entity.setBusiRepeatTypeCountId(vo.getBusiRepeatTypeCountId());
        entity.setConstruction(vo.getConstruction());
        entity.setProjectName(vo.getProjectName());
        entity.setOwner(vo.getOwner());
        entity.setProjectAddress(vo.getProjectAddress());
        entity.setOutputScale(vo.getOutputScale());
        entity.setPublishTime(vo.getPublishTime());
        entity.setBusiInfoId(vo.getBusiInfoId());
        entity.setSimilarityRate(vo.getSimilarityRate());

        entity.insert();
        return entity;
    }

    /**
     * 编辑
     */
    @Transactional(rollbackFor = Exception.class)
    public BusiRepeatTypeCountData editData(BusiRepeatTypeCountDataMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdEdit.class);

        //账值新的对象
        BusiRepeatTypeCountData entity = this.getById(vo.getBusiRepeatTypeCountDataId());
        ObjectUtils.isNullThrow(entity, "数据不存在");

        entity.setBusiRepeatTypeCountId(vo.getBusiRepeatTypeCountId());
        entity.setConstruction(vo.getConstruction());
        entity.setProjectName(vo.getProjectName());
        entity.setOwner(vo.getOwner());
        entity.setProjectAddress(vo.getProjectAddress());
        entity.setOutputScale(vo.getOutputScale());
        entity.setPublishTime(vo.getPublishTime());
        entity.setBusiInfoId(vo.getBusiInfoId());
        entity.setSimilarityRate(vo.getSimilarityRate());

        entity.updateById();
        return entity;
    }


    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void delData(String busiRepeatTypeCountDataId) {
        BusiRepeatTypeCountData data = this.getById(busiRepeatTypeCountDataId);
        if (data == null) {
            return;
        }
        data.deleteById();
    }

    @Transactional(rollbackFor = Exception.class)
    public void delByBusiId(String busiId) {
        this.lambdaUpdate().eq(BusiRepeatTypeCountData::getBusiInfoId,busiId).remove();
    }

    /**
     * 获取原始数据详情
     */
    public BusiRepeatTypeCountDataMapstruct.BusiRepeatTypeCountDataVo getDetailVo(String busiRepeatTypeCountDataId) {
        BusiRepeatTypeCountData entity = this.getById(busiRepeatTypeCountDataId);
        BusiRepeatTypeCountDataMapstruct.BusiRepeatTypeCountDataVo vo = BusiRepeatTypeCountDataMapstruct.INSTANCE.toBusiRepeatTypeCountDataVo(entity);
        return vo;
    }

    /**
     * @description: 查看重复信息列表
     * @author: yinzhangxiao
     * @date: 2024/11/17 16:31
     */
    public List<BusiRepeatTypeCountDataMapstruct.BusiRepeatTypeCountDataVo> getRepeatData(RequestPage page,EnumBusinessRepeatType repeatType, String busiInfoId) {
        BusiRepeatTypeCount repeatTypeCount = baseBusiRepeatTypeCountService.lambdaQuery()
                .select(BusiRepeatTypeCount::getBusiRepeatTypeCountId)
                .eq(BusiRepeatTypeCount::getBusiInfoId, busiInfoId)
                .eq(BusiRepeatTypeCount::getRepeatType, repeatType)
                .one();
        if (repeatTypeCount == null) {
            return new ArrayList<>();
        }
        this.lambdaQuery()
                .eq(BusiRepeatTypeCountData::getBusiRepeatTypeCountId, repeatTypeCount.getBusiRepeatTypeCountId())
                .orderByDesc(BusiRepeatTypeCountData::getSimilarityRate)
                .page(page);
        List<BusiRepeatTypeCountData> dataList = page.getRecords();
        List<BusiRepeatTypeCountDataMapstruct.BusiRepeatTypeCountDataVo> busiRepeatTypeCountDataVoList = BusiRepeatTypeCountDataMapstruct.INSTANCE.toBusiRepeatTypeCountDataVoList(dataList);
        page.setRecords(busiRepeatTypeCountDataVoList);
        return busiRepeatTypeCountDataVoList;
    }
}